<?php

namespace app\common\model;

use think\Model;

class PostClass extends Model
{
    // 关联文章表
    public function post()
    {
        return $this->hasMany('post');
    }
    // 获取文章分类
    public function getPostClassList()
    {
        return $this->field(['id','classname'])->where(['status' => 1])->select();
    }
    // 获取文章分类下的文章列表(分页)
    public function getPost()
    {
        $param = request()->param();
        $userId = request()->userId;
        // 当前用户的id
        $userId = request()->userId ? request()->userId : 0;
        return self::get($param['id'])->post()->with([
            'user' => function($query) use ($userId) {
                return $query->with(['fens' => function($querys) use ($userId){
                    return $querys->alias('fens')->where(['fens.id' => $userId])->field('fens.id,fens.username,fens.userpic')->hidden('pivot');
                },'userinfo'])->field('id,username,userpic');
            },
            'images' => function($query){
                return $query->field('url')->hidden(['pivot']);
            },
            'share','support' => function($query) use ($userId){
                return $query->where('user_id',$userId);
            }])->page($param['page'],10)->select();
    }
}
